package com.mentaleye.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.mentaleye.bean.CommonResult;
import com.mentaleye.bean.PayBean;
import com.mentaleye.enumpackage.AliTradeStatusEnum;
import com.mentaleye.service.PayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PayServiceImpl implements PayService {
    private static Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);
    @Value("${appid}")
    // 商户appid
    private String APPID  ;
    // 私钥 pkcs8格式的
    private String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCOpdu7aBLHsZKMykAUouAYS2x/Z38IoHz1pzGWN7mUScaMEiVKQr0b4v9JLgFGltecydDtCv3bK2FQaNBRLqISfTv4fMbmNosBrgVpLi/mb6ggXVjTvsKR1md9SwvLON1m3eTpLImzRPPhz8pXWnTzCLilGovufOFmsmt0Yhjd5ZTORQ3fQa1k0FN9AAFbGrlNay29IqPxpUT6bc2e8oNVhwlsub2fZzjy4W4q6S3Gi/j2v86J5unHHyn2Ixs/KyoD+DXiVhdg9rmLqqmZO+ccy69Kg7GuJLkuQbQWSBvuaa5gAQBYVAADyp7cPmo9vnaMlx53Fx3OBzhDF/AHwQgzAgMBAAECggEBAIY96gJ8l6fcIYljUFjbSr8yQ8HGOVN6DP71QAIZIQk/QJ1S+krfca5Lumk45TR0Aibn+jkJrgrS0Trl2J19oEJ8HrLjYh8GV1zWUvs/3w4iJ/uFPXHPxLWQK0ZLGKdANYN38NPK+S9/BXCNpuL8GrEYlQOzbtqaJVkLMXqOUs6GtB5XsOmtHxp2/Txx2pHJ2eIPIybuziitZRSFMfuDP1+lSATc6uZMS+/NvnLFt96NTA1nHPwS5U5AH+DBH33sjigGgOKDHzCyKJ+5vDq4fNXtRZQ3yedG/R7pEWVptekFKYGdKAgr5cp5xOJP4ksa2IA14W4PPQd22QpLRVZooBkCgYEA2RuUD9ynLSu6OGjui3sdyPBUr4R6AvhyNuC8jI3UdHUZJ+MpLj+CUm3kZJTOoqJzqVMV2voChGttUs4+2OlEjSoYWg3SpTwP6weHmM0nQTadxV8WYCtAwBd06s8A9/MNhy8L3vESvL3A2Fu2z6SfhMl3tTjmJFhxSSrnvwY+/O8CgYEAqDOZX4gOMU9Ufk890vQpzVGhsF3iF4r8P8GxyqvCIHsKD16qodUOxL1shLFHOV+aqgYmJDZ0oVL0mvppUvri4XxzKp7QjVyULxOUGJHZs0rBt5baZ6J9reTNilinFKEUeZ1KWQGHFEj7oMVjXF+pMTcQrWL/bASp971wL4mv8P0CgYANac/G/xnxB3+U1p7OTUf0zyxcP7IOFu1viIj9ohGbNFTeiwFTUycpEpMGx87Qa3r73h8+jS20orJ5aDanzhmnIw+oGKyH+uL84VU2Uo53T/mFZS+Lt4Bjnc9iZnGn4ANY88Ym+vvuX0bX1yq/P2t/l7bmomJNoo01OQpiWe9wTwKBgQCBkgYjPlbYi2jmSDqy593PyYOJrji9tBYvMxG3s2o+xSlxv5YoRk0zJ0ySR1I7kzc8pLH7EWv03keJnz9FafkehFTfTyoOy3NndGiQKMKQSBVETNQAI383VtszKKnEzXVNXepo+DFLYhFcvtGU1igqvmV9e5aRamJYcrkaFbHQLQKBgGHWrMncHnJGErQK1dru2P5X5StvXBNjSPYmZOgT6EU7an8CSHCM1RH/5CHJ7yRYNhruPoN0mDtkigj8W9680bd9iiF08Rufp5KFwOkyYW3P3GKdI0U0J+olOgNGVaV8Mm2XkMnCJO7iqSf6Q5Fw6Rreu9lM9tQVDMfiQ5TYGlz1";
    // 请求网关地址
    private String URL = "https://openapi.alipaydev.com/gateway.do";
    // 编码
    private String CHARSET = "UTF-8";
    // 返回格式
    private String FORMAT = "json";
    // 支付宝公钥
    private String ALIPAY_PUBLIC_KEY =
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsHuT6gyPi+ht2HULChcl0ARKXfLLAQGMx/frLcPHcI02r853U/5X86CfP001XbRA5/FlzSCId1tEJEbGqYKT9Qi6e2FwE5L7s/YCQp9ezuVvKV10SBiVTb8I0VOwiDTRzV15hShAEAbA8b1yHV2SaNvDBDgnZ6TIs59Ce9LCV1mN1d3n69WEmItiTRdAJh/6puUUdJIcEVACv3UXS5Tz0nY/POnZDYNYgHaYW07mMpPKk3gcwoJ09RbcfHYoMkbXjEIZDZOTCqfS5pJzsx9wsvrirJk8XCuXfb4YzYpnMsgHaabxccmpyjue7GxT4YngD5fzpf02dLRsIMUHk1eSAwIDAQAB";
    // 日志记录目录
    private String log_path = "/log";
    // RSA2
    private String SIGNTYPE = "RSA2";

    @Override
    public CommonResult getPayPage(PayBean payBean) {
        CommonResult result=new CommonResult();
        try {
            AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE);
            AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
            // 封装请求支付信息
            AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
            model.setOutTradeNo(payBean.getOutTradeNo());
            model.setSubject(payBean.getSubject());
            model.setTotalAmount(payBean.getTotalAmount());
            model.setBody(payBean.getBody());
            model.setProductCode("QUICK_WAP_PAY");
            alipay_request.setBizModel(model);
            // 设置异步通知地址
            alipay_request.setNotifyUrl(payBean.getNotifyUrl());
            // 设置同步地址
            alipay_request.setReturnUrl(payBean.getReturnUrl());

            result.setData(client.pageExecute(alipay_request).getBody());
            result.setIsSuccess(true);

        }catch (Exception e){
            logger.error("获取支付页面异常",e);
        }
        return result;
    }

    @Override
    public CommonResult tradeQuery(String outTradeNo) {
        CommonResult result=new CommonResult();
        try {
        AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE);

        AlipayTradeQueryRequest alipay_request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model=new AlipayTradeQueryModel();
        model.setOutTradeNo(outTradeNo);
        alipay_request.setBizModel(model);
        AlipayTradeQueryResponse alipay_response =client.execute(alipay_request);
            String code=alipay_response.getCode();
            if("10000".equals(code)){//返回1000表示接口调用成功
                result.setIsSuccess(true);
                result.setData(AliTradeStatusEnum.getEnum(alipay_response.getTradeStatus()));
            }else if("40004".equals(code)){//40004表示业务错误
                if("ACQ.TRADE_NOT_EXIST".equals(alipay_response.getSubCode())){
                    result.setIsSuccess(true);
                    result.setData(AliTradeStatusEnum.NO_EXSIT);
                }

            }

        }catch (Exception e){
            logger.error("获取支付页面异常",e);
        }
        return result;
    }
}
